package com.hk.crm.workbench.web.controller;

import com.hk.crm.VO.PaginationVO;
import com.hk.crm.settings.domain.User;
import com.hk.crm.settings.service.UserService;
import com.hk.crm.settings.service.impl.UserServiceImpl;
import com.hk.crm.utils.DateTimeUtil;
import com.hk.crm.utils.PrintJson;
import com.hk.crm.utils.ServiceFactory;
import com.hk.crm.utils.UUIDUtil;
import com.hk.crm.workbench.domain.Activity;
import com.hk.crm.workbench.domain.Clue;
import com.hk.crm.workbench.service.ActivityService;
import com.hk.crm.workbench.service.ClueService;
import com.hk.crm.workbench.service.impl.ActivityServiceImpl;
import com.hk.crm.workbench.service.impl.ClueServiceImpl;
import org.apache.ibatis.session.SqlSessionFactory;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ClueController extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入到线索控制器");
        String path=req.getServletPath();
        if ("/workbench/clue/getUserList.do".equals(path)){
            getUserList(req,resp);
        }else if("/workbench/clue/saveClue.do".equals(path)){
            saveClue(req,resp);
        }else if("/workbench/clue/pageList.do".equals(path)){
            pageList(req,resp);
        }else if("/workbench/clue/detail.do".equals(path)){
            detail(req,resp);
        }else if("/workbench/clue/getActivityListByClueId.do".equals(path)){
            getActivityListByClueId(req,resp);
        }else if("/workbench/clue/deleteById.do".equals(path)){
            deleteById(req,resp);
        }else if("/workbench/clue/getActivityListByNameAndNotByClueId.do".equals(path)){
            getActivityListByNameAndNotByClueId(req,resp);
        }

    }

    private void getActivityListByNameAndNotByClueId(HttpServletRequest req, HttpServletResponse resp) {


    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        String id=req.getParameter("id");
        ClueService clueService= (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        boolean flag=clueService.deleteById(id);
        PrintJson.printJsonFlag(resp, flag);
    }

    private void getActivityListByClueId(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("根据线索id查询管理的市场活动列表");
        String clueId=req.getParameter("clueId");
        ActivityService proxyService= (ActivityService) ServiceFactory.getService(new ActivityServiceImpl());
        List<Activity> activityList=proxyService.getActivityListByClueId(clueId);
        PrintJson.printJsonObj(resp, activityList);
    }

    private void detail(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入到跳转线索信息页的操作");
        String id=req.getParameter("id");
        ClueService proxyService = (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        Clue clue=proxyService.detail(id);
//        将得到的c存到request域
        req.setAttribute("clue", clue);

        req.getRequestDispatcher("/workbench/clue/detail.jsp").forward(req, resp);
    }

    private void pageList(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("进入到查询线索");
        String fullname=req.getParameter("fullname");
        String company=req.getParameter("company");
        String phone=req.getParameter("phone");
        String mphone=req.getParameter("mphone");
        String source=req.getParameter("source");
        String owner=req.getParameter("owner");
        String status=req.getParameter("status");
//        代表第几页
        String pageNoStr=req.getParameter("pageNo");
        int pageNo=Integer.valueOf(pageNoStr);
//       代表每页展现几条数据
        String pageSizeStr=req.getParameter("pageSize");
        int pageSize=Integer.valueOf(pageSizeStr);
//        计算出略过的记录数
        int skipCount=(pageNo-1)*pageSize;
        Map<String ,Object> map=new HashMap();
        map.put("fullname",fullname);
        map.put("owner", owner);
        map.put("company", company);
        map.put("phone", phone);
        map.put("mphone", mphone);
        map.put("source", source);
        map.put("status", status);
        map.put("skipCount", skipCount);
        map.put("pageSize", pageSize);
        ClueService proxyService= (ClueService) ServiceFactory.getService(new ClueServiceImpl());
/*
            前端要：市场活动信息列表
            查询的总条数
            业务层拿到了上述两项信息之后如何做返回
                map
                vo
* */
//        Service返回的是个VO，里面是从数据库得到的List<Clue> 和total 数据总数
        PaginationVO<Clue> vo=proxyService.pageList(map);
//        将得到的VO转成json串传给前端
        PrintJson.printJsonObj(resp, vo);
    }

    private void saveClue(HttpServletRequest req, HttpServletResponse resp) {
        String id=UUIDUtil.getUUID();
        String createTime= DateTimeUtil.getSysTime();
        //        创建人：当前登录的用户
        String createBy=((User)req.getSession().getAttribute("user")).getNAME();
        String owner = req.getParameter("owner");
        String company = req.getParameter("company");
        String appellation = req.getParameter("appellation");
        String fullname = req.getParameter("fullname");
        String job = req.getParameter("job");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String website = req.getParameter("website");
        String mphone = req.getParameter("mphone");
        String status = req.getParameter("status");
        String source = req.getParameter("source");
        String description = req.getParameter("description");
        String contactSummary = req.getParameter("contactSummary");
        String nextContactTime = req.getParameter("nextContactTime");
        String address = req.getParameter("address");
        Clue clue=new Clue();
        clue.setId(id);
        clue.setAddress(address);
        clue.setAppellation(appellation);
        clue.setCompany(company);
        clue.setContactSummary(contactSummary);
        clue.setCreateBy(createBy);
        clue.setCreateTime(createTime);
        clue.setDescription(description);
        clue.setEmail(email);
        clue.setOwner(owner);
        clue.setFullname(fullname);
        clue.setJob(job);
        clue.setPhone(phone);
        clue.setWebsite(website);
        clue.setMphone(mphone);
        clue.setState(status);
        clue.setSource(source);
        clue.setNextContactTime(nextContactTime);
        ClueService clueService= (ClueService) ServiceFactory.getService(new ClueServiceImpl());
        boolean flag=clueService.saveClue(clue);
        Map<String ,Object> map=new HashMap<>();
        map.put("success", flag);
        map.put("clue", clue);
        PrintJson.printJsonObj(resp, map);
    }

    private void getUserList(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("取得用户信息列表");
        UserService userService= (UserService) ServiceFactory.getService(new UserServiceImpl());
        List<User> userList = userService.getUserList();
        PrintJson.printJsonObj(resp, userList);
    }


}
